package com.glodon.paas.task.document;

import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.glodon.paas.task.TaskConfig;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class DocumentServiceTask {
	
	private static Logger logger = LoggerFactory.getLogger(DocumentServiceTask.class);
	
	private boolean runDelFiles = false;
	
	private int expiredFiles = 1;
	
	private int nDaysBefore = 0;

	public void deleteFile(){
		logger.info("start delete expired("+nDaysBefore+" days before) files ");
		while(runDelFiles){
			WebResource resource = Client.create().resource(TaskConfig.API_INTERNAL_DOCUMENT_URL+"/cleanExpiredFiles?nDaysBefore="+nDaysBefore+"&expiredFiles="+expiredFiles);
			ClientResponse c = resource.type(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
			if (c.getStatus() == 204) {
				break;
			}
			else{
				int count = Integer.valueOf(c.getEntity(String.class)).intValue();
				logger.info("deleted "+count+"files");
				if(count <=0){
					break;
				}
			}
		}
		logger.info("end delete expired("+nDaysBefore+" days before) files ");
	}

	public void setRunDelFiles(boolean runDelFiles) {
		this.runDelFiles = runDelFiles;
	}

	public void setnDaysBefore(int nDaysBefore) {
		this.nDaysBefore = nDaysBefore;
	}

	public void setExpiredFiles(int expiredFiles) {
		this.expiredFiles = expiredFiles;
	}
}
